package com.slt.android.customview;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by dp on 2017/12/18.
 */

public class CustomScroolView extends ViewGroup {


    private int sh;
    private int sw;


    private int mLastY , mStart , mEnd;
    private Scroller mScroller;

    public CustomScroolView(Context context) {
        super(context);
        init(context);
    }


    public CustomScroolView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomScroolView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        float density = dm.density;
        sw = dm.widthPixels;
        sh = dm.heightPixels;
        mScroller = new Scroller(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            measureChild(childView , widthMeasureSpec , heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        //设置viewGroup高度
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = sh * childCount;
        setLayoutParams(mlp);

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if(child.getVisibility() != View.GONE){
                child.layout(l , i*sh , r , (i+1)* sh );
            }
        }
        
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastY = y;
                mStart = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(!mScroller.isFinished()){
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if(getScrollY() < 0){
                    dy = 0;
                }
                if(getScrollY() > getHeight() - sh){
                    dy = 0;
                }
                scrollBy(0 , dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if(dScrollY > 0){
                    if(dScrollY < sh/3){
                        mScroller.startScroll(0 , getScrollY() , 0 , -dScrollY);
                    }else {
                        mScroller.startScroll(0 , getScrollY() , 0 , sh - dScrollY);
                    }
                }else {
                    if(-dScrollY < sh/3){
                        mScroller.startScroll(0 , getScrollY() , 0 , -dScrollY);
                    }else {
                        mScroller.startScroll(0 , getScrollY() , 0 , -sh - dScrollY);
                    }
                }
                break;
            default:
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(mScroller.computeScrollOffset()){
            scrollTo(0 , mScroller.getCurrY());
            postInvalidate();
        }
    }
}
